package com.zw.pm.module.board.single;

import com.zw.pm.base.BaseController;
import com.zw.pm.common.JsonResult;
import com.zw.pm.gen.entity.*;
import com.zw.pm.module.system.log.LogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 单项目 控制层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Controller
@RequestMapping("/board/single")
public class SingleController extends BaseController {

    @Autowired
    private SingleService singleService;
    @Autowired
    private LogService logService;

    /**
     * 单项目页面
     * 
     * @return 页面路径
     */
    @GetMapping("/{id}")
    @RequiresAuthentication
    public String project(@PathVariable("id") Integer id, Model model) {
        return getProject(id, model);
    }

    /**
     * 单项目看板数据
     *
     * @param id 项目ID
     * @param model model
     * @return 路径
     */
    private String getProject(Integer id, Model model) {
        model.addAttribute("project", singleService.getProjectById(id));
        model.addAttribute("messages", singleService.findMessagesByProjectId(id));
        model.addAttribute("sendTask", singleService.findTaskByProjectIdAndTaskTempId(id,1));
        model.addAttribute("processTempTask", singleService.findTaskByProjectIdAndTaskTempId(id,2));
        model.addAttribute("processBackTask", singleService.findTaskByProjectIdAndTaskTempId(id,3));
        model.addAttribute("consumeTask", singleService.findTaskByProjectIdAndTaskTempId(id,4));
        model.addAttribute("sprayTask", singleService.findTaskByProjectIdAndTaskTempId(id,5));
        model.addAttribute("packTask", singleService.findTaskByProjectIdAndTaskTempId(id,6));
        model.addAttribute("storeTask", singleService.findTaskByProjectIdAndTaskTempId(id,7));
        model.addAttribute("factoryTasks", singleService.findFactoryTaskByProjectId(id));
        return "board/single/page";
    }

    /**
     * 单项目页面
     *
     * @return 页面路径
     */
    @GetMapping("/{id}/{msgId}")
    @RequiresAuthentication
    public String project(@PathVariable("id") Integer id, @PathVariable("msgId") Integer msgId, Model model) {
        singleService.handlerMessage(msgId);
        return getProject(id, model);
    }

    /**
     * 重点项目设置
     *
     * @param project project
     * @return json
     */
    @ResponseBody
    @PostMapping("/focus")
    @RequiresPermissions({"single:focus"})
    public JsonResult updateFocus(Project project) {
        Project p = singleService.getProjectById(project.getId());
        logService.saveLog(( project.getFocus() == 0 ? "取消重点项目" : "重点项目"),  "项目名：" + p.getName());
        return jsonResult(singleService.updateProject(project));
    }

    /**
     * 查询所有用户
     *
     * @return list
     */
    @ResponseBody
    @GetMapping("/users")
    @RequiresAuthentication
    public List<User> users() {
        return singleService.findUsers();
    }

    /**
     * 发消息
     *
     * @param message message
     * @return json
     */
    @ResponseBody
    @PostMapping("/sendMessage")
    @RequiresAuthentication
    public JsonResult sendMessage(Message message) {
        Project p = singleService.getProjectById(message.getProjectId());
        logService.saveLog("发消息", "项目名：" + p.getName() + "<br>内容：" + message.getContent());
        singleService.sendMessage(message);
        return success();
    }

    /**
     * 根据ID查询任务
     *
     * @param id id
     * @return json
     */
    @ResponseBody
    @PostMapping("/task/id")
    @RequiresAuthentication
    public TaskModel taskById(Integer id) {
        return singleService.getTaskById(id);
    }

    /**
     * 修改任务
     *
     * @param task task
     * @return json
     */
    @ResponseBody
    @PostMapping("/task/edit")
    @RequiresPermissions(value = {"single:edit:process", "single:edit:produce",
            "single:edit:consume", "single:edit:spray", "single:edit:pack",
            "single:edit:store"},logical = Logical.OR)
    public JsonResult editTask(Task task) {
        TaskModel t = singleService.getTaskById(task.getId());
        Project p = singleService.getProjectById(t.getProjectId());
        logService.saveLog("编辑任务<br>项目名：" + p.getName() + "<br>任务名：" + t.getAlias(), t, task);
        return jsonResult(singleService.editTask(task));
    }

    /**
     * 修改任务进度,，废弃
     *
     * @param task task
     * @return json
     */
    @Deprecated
    @ResponseBody
    @PostMapping("/task/status")
    @RequiresPermissions(value = {"single:edit:process", "single:edit:produce",
            "single:edit:consume", "single:edit:spray", "single:edit:pack",
            "single:edit:store"},logical = Logical.OR)
    public JsonResult taskStatus(Task task) {
        return jsonResult(singleService.taskStatus(task));
    }

    /**
     * 撤消任务进度
     *
     * @param task task
     * @return json
     */
    @ResponseBody
    @PostMapping("/task/cancel")
    @RequiresPermissions(value = {"single:edit:process", "single:edit:produce",
            "single:edit:consume", "single:edit:spray", "single:edit:pack",
            "single:edit:store"},logical = Logical.OR)
    public int taskCancel(Task task) {
        return singleService.taskCancel(task);
    }

    /**
     * 根据ID查询任务文件
     *
     * @param id id
     * @return list
     */
    @ResponseBody
    @PostMapping("/task/file")
    @RequiresAuthentication
    public List<Doc> files(Integer id) {
        return singleService.findDocByTaskId(id);
    }

    /**
     * 上传文件
     *
     * @param doc doc
     * @param file 文件
     * @return json
     */
    @ResponseBody
    @PostMapping("/file/upload")
    @RequiresPermissions(value = {"single:upload:send", "single:upload:process",
            "single:upload:consume", "single:upload:spray", "single:upload:pack",
            "single:upload:store", "single:upload:produce"},logical = Logical.OR)
    public JsonResult upload(Doc doc, @RequestParam("file") MultipartFile[] file) {
        return jsonResult(singleService.upload(doc, file));
    }

    /**
     * 文件下载
     *
     * @param response response
     * @param id id
     */
    @GetMapping("/file/download")
    @RequiresPermissions(value = {"single:download:send","single:download:process","single:download:consume",
            "single:download:spray","single:download:pack","single:download:store", "single:download:produce"},logical = Logical.OR)
    public void download(HttpServletResponse response, @RequestParam("id") Integer[] id) {
        singleService.download(response, id);
    }

    /**
     * 删除文件
     *
     * @param id id 数组
     * @return json
     */
    @ResponseBody
    @PostMapping("/file/del")
    @RequiresPermissions(value = {"single:upload:send", "single:upload:process",
            "single:upload:consume", "single:upload:spray", "single:upload:pack",
            "single:upload:store", "single:upload:produce"},logical = Logical.OR)
    public JsonResult delFile(@RequestParam("id") Integer[] id) {
        return jsonResult(singleService.delFile(id));
    }

}
